package com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.importpayment.domain.dao;

import com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.importpayment.domain.entity.ImportPaymentSimple;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lianhong
 * @since 2021/11/19
 */
@Repository
public class ImportPaymentSimpleDao {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String saveImportPaymentSimpleSql = "INSERT INTO e_import_payment_simple (entryId, dDate) VALUES (:entryId, :dDate)";

    public void save(ImportPaymentSimple simple) {
        namedParameterJdbcTemplate.update(this.saveImportPaymentSimpleSql, new BeanPropertySqlParameterSource(simple));
    }

    private String deleteImportPaymentSimpleSql = "DELETE FROM e_import_payment_simple WHERE entryId = ?";

    public void delete(String entryId) {
        this.namedParameterJdbcTemplate.getJdbcOperations().update(this.deleteImportPaymentSimpleSql, entryId);
    }

    private String existsImportPaymentSql = "SELECT count(1) FROM e_import_payment_simple WHERE entryId = ?";

    public boolean exists(String entryId) {
        Integer count = namedParameterJdbcTemplate.getJdbcOperations()
                .queryForObject(this.existsImportPaymentSql, new Object[]{entryId}, Integer.class);
        return count != null && count > 0;
    }

    private String existsImportPaymentSimpleSql = "SELECT entryId FROM e_import_payment_simple WHERE entryId in (:entryIds)";

    public List<String> exists(List<String> entryIds) {
        Map<String, Object> params = new HashMap<>();
        params.put("entryIds", entryIds);
        return namedParameterJdbcTemplate.queryForList(this.existsImportPaymentSimpleSql, params, String.class);
    }
}
